const PriorityQueue = require("../template")

function testPriorityQueueComplexScenarios() {
  console.log(
    "1. 复杂数据类型处理测试:",
    (() => {
      const pq = new PriorityQueue((a, b) => a.priority - b.priority)
      
      // 测试各种复杂数据类型
      const complexItems = [
        { name: 'task1', priority: 3, data: { id: 1, value: 'high' } },
        { name: 'task2', priority: 1, data: { id: 2, value: 'low' } },
        { name: 'task3', priority: 2, data: { id: 3, value: 'medium' } },
        { name: 'task4', priority: 4, data: { id: 4, value: 'urgent' } },
        { name: 'task5', priority: 1, data: { id: 5, value: 'critical' } }
      ]
      
      complexItems.forEach(item => pq.enqueue(item))
      
      // 验证所有复杂数据都能正确存储和查询
      const first = pq.dequeue()
      const second = pq.peek()
      
      return first.name === 'task2' && first.priority === 1 &&
             second.name === 'task5' && second.priority === 1 &&
             pq.size() === 4
    })()
  )

  console.log(
    "2. 大量数据管理测试:",
    (() => {
      const pq = new PriorityQueue()
      const count = 5000
      
      // 插入大量数据
      for (let i = 0; i < count; i++) {
        pq.enqueue(i)
      }
      
      // 随机访问测试
      const randomIndices = Array.from({ length: 100 }, () => 
        Math.floor(Math.random() * count)
      )
      
      // 验证堆性质
      const isValid = pq.isValid()
      const min = pq.peek()
      const size = pq.size()
      
      return isValid === true &&
             min === 0 &&
             size === count
    })()
  )

  console.log(
    "3. 动态负载测试:",
    (() => {
      const pq = new PriorityQueue()
      
      // 阶段1：插入数据
      for (let i = 0; i < 1000; i++) {
        pq.enqueue(i)
      }
      
      const size1 = pq.size()
      const isValid1 = pq.isValid()
      
      // 阶段2：删除部分数据
      for (let i = 0; i < 500; i++) {
        pq.dequeue()
      }
      
      const size2 = pq.size()
      const isValid2 = pq.isValid()
      
      // 阶段3：重新插入
      for (let i = 0; i < 500; i++) {
        pq.enqueue(i)
      }
      
      const size3 = pq.size()
      const isValid3 = pq.isValid()
      
      return isValid1 === true &&
             isValid2 === true &&
             isValid3 === true &&
             size1 === 1000 &&
             size2 === 500 &&
             size3 === 1000
    })()
  )

  console.log(
    "4. 并发操作模拟测试:",
    (() => {
      const pq = new PriorityQueue()
      
      // 模拟多个操作同时进行
      const operations = []
      
      // 操作1：批量插入
      for (let i = 0; i < 100; i++) {
        operations.push(() => pq.enqueue(i))
      }
      
      // 操作2：批量查看
      for (let i = 0; i < 100; i++) {
        operations.push(() => pq.peek())
      }
      
      // 操作3：批量删除
      for (let i = 0; i < 50; i++) {
        operations.push(() => pq.dequeue())
      }
      
      // 随机执行操作
      const shuffledOps = operations.sort(() => Math.random() - 0.5)
      shuffledOps.forEach(op => op())
      
      // 验证最终状态
      const finalSize = pq.size()
      const isValid = pq.isValid()
      
      return finalSize === 50 && // 100 - 50 = 50
             isValid === true
    })()
  )

  console.log(
    "5. 边界条件处理测试:",
    (() => {
      const pq = new PriorityQueue()
      
      // 测试边界条件
      pq.enqueue(Number.MAX_SAFE_INTEGER)
      pq.enqueue(Number.MIN_SAFE_INTEGER)
      pq.enqueue(0)
      pq.enqueue(-1)
      pq.enqueue(1)
      
      // 验证所有边界值都能处理
      const min = pq.peek()
      const first = pq.dequeue()
      const second = pq.peek()
      
      return min === Number.MIN_SAFE_INTEGER &&
             first === Number.MIN_SAFE_INTEGER &&
             second === -1 &&
             pq.size() === 4 &&
             pq.isValid() === true
    })()
  )

  console.log(
    "6. 错误恢复测试:",
    (() => {
      const pq = new PriorityQueue()
      
      // 正常操作
      pq.enqueue(50)
      pq.enqueue(30)
      pq.enqueue(70)
      
      // 尝试一些边界操作
      pq.enqueue(50) // 重复插入
      pq.dequeue() // 正常删除
      
      // 验证状态
      const size = pq.size()
      const isValid = pq.isValid()
      const min = pq.peek()
      
      return size === 3 &&
             isValid === true &&
             min === 30
    })()
  )

  console.log(
    "7. 内存效率测试:",
    (() => {
      const pq = new PriorityQueue()
      
      // 添加大量数据测试内存效率
      const startMemory = process.memoryUsage ? process.memoryUsage().heapUsed : 0
      
      for (let i = 0; i < 2000; i++) {
        pq.enqueue(i)
      }
      
      const endMemory = process.memoryUsage ? process.memoryUsage().heapUsed : 0
      const memoryUsed = endMemory - startMemory
      
      // 验证统计信息
      const size = pq.size()
      const isValid = pq.isValid()
      
      return pq.size() === 2000 &&
             isValid === true &&
             memoryUsed < 50 * 1024 * 1024 // 内存使用应该合理
    })()
  )

  console.log(
    "8. 堆重建测试:",
    (() => {
      const pq = new PriorityQueue()
      
      // 插入数据
      const values = [50, 30, 70, 20, 40, 60, 80, 10, 25, 35]
      values.forEach(val => pq.enqueue(val))
      
      // 删除一些元素
      pq.dequeue()
      pq.dequeue()
      pq.dequeue()
      
      // 验证堆仍然有效
      const isValid = pq.isValid()
      const remainingMin = pq.peek()
      const size = pq.size()
      
      return isValid === true &&
             remainingMin === 25 &&
             size === 7
    })()
  )

  console.log(
    "9. 自定义比较器复杂测试:",
    (() => {
      const pq = new PriorityQueue((a, b) => {
        // 复杂的比较逻辑
        if (a.priority !== b.priority) return a.priority - b.priority
        if (a.value !== b.value) return a.value - b.value
        return a.id - b.id
      })
      
      pq.enqueue({ id: 1, priority: 2, value: 10 })
      pq.enqueue({ id: 2, priority: 1, value: 5 })
      pq.enqueue({ id: 3, priority: 1, value: 5 })
      pq.enqueue({ id: 4, priority: 2, value: 5 })
      
      const first = pq.dequeue()
      const second = pq.dequeue()
      const third = pq.peek()
      
      return first.id === 2 && first.priority === 1 && first.value === 5 &&
             second.id === 3 && second.priority === 1 && second.value === 5 &&
             third.id === 4 && third.priority === 2 && third.value === 5 &&
             pq.isValid() === true &&
             pq.size() === 2
    })()
  )

  console.log(
    "10. this上下文复杂场景测试:",
    (() => {
      const complexObj = {
        multiplier: 3,
        processComplex: function(values) {
          const pq = new PriorityQueue()
          values.forEach(val => pq.enqueue(val))
          return pq.size() * this.multiplier
        }
      }
      
      const testValues = [10, 20, 30, 40, 50]
      const result = complexObj.processComplex(testValues)
      
      return result === 15 // 5 * 3 = 15
    })()
  )

  console.log(
    "11. 复杂对象this绑定场景测试:",
    (() => {
      const complexConfigObj = {
        config: { 
          threshold: 3, 
          maxSize: 10 
        },
        processWithComplexConfig: function(values) {
          const pq = new PriorityQueue()
          values.forEach(val => pq.enqueue(val))
          return pq.size() >= this.config.threshold &&
                 pq.size() <= this.config.maxSize
        }
      }
      
      const testValues = [10, 20, 30, 40, 50]
      return complexConfigObj.processWithComplexConfig(testValues) === true
    })()
  )

  console.log(
    "12. 综合复杂场景测试:",
    (() => {
      const pq = new PriorityQueue()
      
      // 综合测试：多种操作混合
      const operations = []
      
      // 插入各种数据
      for (let i = 0; i < 100; i++) {
        operations.push(() => pq.enqueue(i))
        operations.push(() => pq.enqueue(i + 1000)) // 大数值
        operations.push(() => pq.enqueue(-i)) // 负数值
      }
      
      // 执行操作
      operations.forEach(op => op())
      
      // 删除部分数据
      for (let i = 0; i < 50; i++) {
        pq.dequeue()
      }
      
      // 验证最终状态
      const finalStats = {
        size: pq.size(),
        isValid: pq.isValid(),
        min: pq.peek()
      }
      
      // 验证所有统计信息都合理
      return finalStats.size === 250 && // 300 - 50 = 250
             finalStats.isValid === true &&
             finalStats.min === -49 && // 最小的负值
             typeof finalStats.size === 'number' &&
             typeof finalStats.isValid === 'boolean' &&
             typeof finalStats.min === 'number'
    })()
  )
}

testPriorityQueueComplexScenarios()
